package com.zbkj.front.controller;

import com.zbkj.dal.base.constants.DateConstants;
import com.zbkj.dal.base.enums.PickupAppointmentAvailableTime;
import com.zbkj.dal.base.page.CommonPage;
import com.zbkj.dal.base.result.CommonResult;
import com.zbkj.dal.pojo.request.PageParamRequest;
import com.zbkj.dal.pojo.response.ba.MyShareResponse;
import com.zbkj.dal.pojo.response.ba.ShareDetailResponse;
import com.zbkj.dal.pojo.response.ba.ShareOrderCancelResponse;
import com.zbkj.front.model.req.ArriveStoreAfterInitialEvaluationReq;
import com.zbkj.front.model.req.OrderAddressModifyReq;
import com.zbkj.front.model.req.PickupAfterInitialEvaluationReq;
import com.zbkj.front.model.req.PickupModifyAfterInitialEvaluationReq;
import com.zbkj.service.model.vo.ArriveStoreAddressVO;
import com.zbkj.front.model.vo.PickupAppointmentAvailableTimeVO;
import com.zbkj.front.model.vo.SharePickupInfoVO;
import com.zbkj.front.model.vo.SupplyEstimateConfirmVO;
import com.zbkj.front.service.ApiSheShareService;
import com.zbkj.service.model.front.req.recycle.SupplyApplyCancelReq;
import com.zbkj.service.model.front.req.recycle.SupplyApplyReq;
import com.zbkj.service.service.ShareService;
import com.zbkj.service.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("api/front/supply")
@Api(tags = "奢品供享")
public class SheShareController {
    @Autowired
    private ShareService shareService;
    @Autowired
    private UserService userService;
    @Autowired
    private ApiSheShareService sheShareService;

    @ApiOperation(value = "提交奢品供享信息", tags = {"供享订单", "供享订单创建"})
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult<Boolean> create(@Validated @RequestBody SupplyApplyReq shareRequest) {
        return CommonResult.success(shareService.create(shareRequest));
    }

    @ApiOperation(value = "我的奢品供享列表", tags = "供享订单")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<MyShareResponse>> list(String status, PageParamRequest pageParamRequest) {
        return CommonResult.success(shareService.listMyShare(status, pageParamRequest));
    }

    @ApiOperation(value = "取消奢品供享", tags = "供享订单")
    @PostMapping(value = "/cancel")
    public CommonResult<ShareOrderCancelResponse> cancel(@Validated @RequestBody SupplyApplyCancelReq req) {
        return CommonResult.success(shareService.cancel(req));
    }

    @ApiOperation(value = "奢品供享信息详情", tags = "供享订单")
    @RequestMapping(value = "/detail/{orderNo}", method = RequestMethod.GET)
    public CommonResult<ShareDetailResponse> detail(@PathVariable("orderNo") String orderNo) {
        Integer userId = userService.getUserIdException();
        ShareDetailResponse share = shareService.detail(orderNo, userId);
        return CommonResult.success(share);
    }

    @ApiOperation(value = "供享退回 - 确认收货", tags = "供享订单")
    @GetMapping(value = "/return/delivery-confirm")
    public CommonResult<?> returnDeliveryConfirm(@RequestParam("orderNo") String orderNo) {
        shareService.returnDeliveryConfirm(orderNo);
        return CommonResult.success();
    }


//    @ApiOperation(value = "删除我的奢品供享信息", tags = "供享订单", notes = "只能删已提交的")
//    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
//    public CommonResult<Boolean> delete(@PathVariable("id") Integer id) {
//        return CommonResult.success(shareService.deleteMyShare(id));
//    }

    // ----------------------------- 快递取件 -----------------------------

    @ApiOperation(value = "获取上门取件可预约时间", notes = "KEY: 标识， VAL: 11:00 - 13:00")
    @GetMapping("/pickup/appointment/available-time")
    private CommonResult<List<PickupAppointmentAvailableTimeVO>> availableTime() {
        List<PickupAppointmentAvailableTimeVO> collect = Arrays.stream(PickupAppointmentAvailableTime.values()).map(value -> {
            PickupAppointmentAvailableTimeVO pickupAppointmentAvailableTimeVO = new PickupAppointmentAvailableTimeVO();
            pickupAppointmentAvailableTimeVO.setCode(value.getCode());
            pickupAppointmentAvailableTimeVO.setTime(value.getStart().format(DateConstants.DATETIME_FORMATTER_HHMM) + " - " + value.getEnd().format(DateConstants.DATETIME_FORMATTER_HHMM));
            pickupAppointmentAvailableTimeVO.setIsDefault(false);
            return pickupAppointmentAvailableTimeVO;
        }).collect(Collectors.toList());
        return CommonResult.success(collect);
    }

    @ApiOperation(value = "预约上门取件 - 初评结果之后", tags = "供享订单")
    @RequestMapping(value = "/pickup", method = RequestMethod.POST)
    public CommonResult<ShareDetailResponse.PickUpAppt> pickupAfterInitialEvaluation(@RequestBody PickupAfterInitialEvaluationReq req) {
        Integer userId = userService.getUserIdException();
        return CommonResult.success(sheShareService.pickupAfterInitialEvaluation(req, userId));
    }

    @ApiOperation(value = "预约上门取件 - 取消寄件", tags = "供享订单")
    @GetMapping(value = "/pickup/cancel")
    public CommonResult<?> pickupCancel(@RequestParam("orderNo") String orderNo) {
        sheShareService.pickupCancel(orderNo);
        return CommonResult.success();
    }

    @ApiOperation(value = "预约上门取件 - 修改取件信息", tags = "供享订单")
    @PostMapping(value = "/pickup/update")
    public CommonResult<?> pickupUpdate(@RequestBody PickupModifyAfterInitialEvaluationReq req) {
        sheShareService.pickupUpdate(req);
        return CommonResult.success();
    }

    @ApiOperation(value = "预约取件信息", tags = "供享订单")
    @RequestMapping(value = "/pickup/info", method = RequestMethod.POST)
    public CommonResult<SharePickupInfoVO> pickupInfo(@RequestParam("orderNo") String orderNo) {
        return CommonResult.success(sheShareService.pickupInfo(orderNo));
    }


    // ----------------------------- 到店估价 -----------------------------

    @ApiOperation(value = "到店估价 - 预约到店时间 - 初评结果之后", tags = "供享订单")
    @RequestMapping(value = "/store", method = RequestMethod.POST)
    public CommonResult<ShareDetailResponse.InStoreAppt> storeAfterInitialEvaluation(@RequestBody ArriveStoreAfterInitialEvaluationReq req) {
        Integer userId = userService.getUserIdException();
        return CommonResult.success(sheShareService.arriveStoreAfterInitialEvaluationReq(req, userId));
    }

    @ApiOperation(value = "到店估价 - 取消到店", tags = "供享订单")
    @GetMapping(value = "/store/cancel")
    public CommonResult<?> storeCancel(@RequestParam String orderNo) {
        sheShareService.arriveStoreCancel(orderNo);
        return CommonResult.success();
    }

    @ApiOperation(value = "到店估价 - 修改到店信息", tags = "供享订单")
    @PostMapping(value = "/store/update")
    public CommonResult<?> storeUpdate(@RequestBody ArriveStoreAfterInitialEvaluationReq req) {
        Integer userId = userService.getUserIdException();
        sheShareService.arriveStoreUpdate(req, userId);
        return CommonResult.success();
    }

    @ApiOperation(value = "到店估价 - 店铺地址信息", tags = "供享订单")
    @GetMapping(value = "/store/address")
    public CommonResult<ArriveStoreAddressVO> storeAddress() {
        return CommonResult.success(sheShareService.evaluationStroeAddress());
    }

    // ----------------------------- 确认估价 -----------------------------

    @ApiOperation(value = "确认估价 - 确认估价", tags = "供享订单")
    @GetMapping(value = "/estimate/confirm")
    public CommonResult<SupplyEstimateConfirmVO> estimateConfirm(@RequestParam String orderNo) {
        Integer userId = userService.getUserIdException();
        return CommonResult.success(sheShareService.estimateConfirm(orderNo, userId));
    }


}
